package eg.edu.eelu.fyp2013.jdetector.core.swt;

import java.util.ArrayList;

import com.mathworks.toolbox.javabuilder.MWException;
import com.mathworks.toolbox.javabuilder.MWNumericArray;

import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;
import matlabcontrol.extensions.MatlabNumericArray;
import matlabcontrol.extensions.MatlabTypeConverter;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.ClassiferData;
import KLDANeural.*;

public class KLDANeuralPCA {
	
	
	public ClassiferData [] learnedreduced;
    public ClassiferData [] Testedreduced ;
    
    public ClassiferData [] getlearnedred ()
    {
    	return learnedreduced;
    }
    public ClassiferData [] gttestedred()
    {
    	return Testedreduced;
    }
    
	
	public void classifykernalplusdistance (ClassiferData [] Learned , ClassiferData [] Tested 
			
			) throws  MWException
	{
	
		
		double [][] LearnedMatrix = new double [Learned.length][Learned[0].Allreduceddata.size()];
        double [][] learnedlabels = new double [Learned.length][1];
		for(int i = 0; i < Learned.length; i++)
		{
			for(int j = 0; j < Learned[i].Allreduceddata.size(); j++)
			{
				LearnedMatrix[i][j] = Learned[i].Allreduceddata.get(j);
			}
		    learnedlabels[i][0] = Learned[i].label; 
			
		}
		
		double [][] TestedMatrix = new double [Tested.length][Tested[0].Allreduceddata.size()];

		for(int i = 0; i < Tested.length; i++)
		{
			for(int j = 0; j < Tested[i].Allreduceddata.size(); j++)
			{
				TestedMatrix[i][j] = Tested[i].Allreduceddata.get(j);
			}
			
		}
		
		
		  Object [] KD = new Object[3];
		  NKLDA Fklda = new NKLDA(); 
		  KD[0] = LearnedMatrix;
		  KD[1] = TestedMatrix;
		  KD[2] = learnedlabels;
		  
		        
		  MWNumericArray [] M = new MWNumericArray[2];
		  Fklda.KLDAPCANeural(M, KD); 
		  double [] learnedvalues = M[0].getDoubleData();
		  double [] testedvalues =  M[1].getDoubleData();
		  
	   // processor.setNumericArray("Learn", new MatlabNumericArray(LearnedMatrix,null));
	   // processor.setNumericArray("Test", new MatlabNumericArray(TestedMatrix,null));
	   // processor.setNumericArray("labels", new MatlabNumericArray(learnedlabels,null));
		   
	    
	   
	   // proxy.eval("[X , Y] = KLDAPCA(Learn,Test,labels)");
	    
	  //  double [][] learnedvalues = processor.getNumericArray("X").getRealArray2D();
	  //  double [][] testedvalues = processor.getNumericArray("Y").getRealArray2D();
	     
	    System.out.println("");
	    learnedreduced = new ClassiferData[Learned.length];
	    Testedreduced = new ClassiferData[Tested.length];
	    
	   
	    
	    for(int i = 0; i < Learned.length; i++)
	    {
	    	ClassiferData CD = new ClassiferData();
	    	CD.Allreduceddata = new ArrayList<Double>();
	    	CD.Allreduceddata.add(learnedvalues[i]);
	    	CD.label = Learned[i].label;
	    	
	    	learnedreduced[i] = CD;
	    	
	    }
	    
	    for(int i = 0; i < Tested.length; i++)
	    {
	    	ClassiferData CD = new ClassiferData();
	    	CD.Allreduceddata = new ArrayList<Double>();
	    	CD.Allreduceddata.add(testedvalues[i]);
	    	CD.label = Tested[i].label;
	 	    Testedreduced[i] = CD;
	    	
	    }
	    
	    System.out.print("");  
	}
	    
	  

}
